
#ifndef __KERNEL__
#  define __KERNEL__
#endif
#ifndef MODULE
#  define MODULE
#endif
#include <linux/config.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/tcp.h>
#include <linux/ip.h>
#include <net/ip.h>


#define u16toValue(x)\
			((__u16) \
			((((__u16)(x) & (__u16)0x00ffU) << 8) | \
			(((__u16)(x) & (__u16)0xff00U) >> 8) ))
#define u32toValue(x) \
			((__u32)( \
			(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
			(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
			(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
			(((__u32)(x) & (__u32)0xff000000UL) >> 24) ))

static IpAddressToString(unsigned int i,char *s)
{
    unsigned int j1,j2,j3,j4;
    j1=i&0x000000FF;
    j2=(i&0x0000FF00)>>8;
    j3=(i&0x00FF0000)>>16;
    j4=(i&0xFF000000)>>24;
    sprintf(s,"%u.%u.%u.%u",j4,j3,j2,j1);
}

extern int (*eeFunction02)(struct sk_buff *);

int PacketInformation(struct sk_buff *skb)
{
    struct iphdr *iph;
    struct tcphdr *th;
    char SourceIP[50],TargetIP[50];
    static int PacketNumber=0;

    iph = skb->nh.iph;
    
    printk("--------------------------------- PacketNumber=%d\n",PacketNumber++);
    printk("IP:version=0x%X\n",skb->nh.iph->version);
    printk("IP:ip header length=0x%X\n",skb->nh.iph->ihl);
    printk("IP:tos=0x%X\n",skb->nh.iph->tos);
    printk("IP:tot_len=%d\n",u16toValue(skb->nh.iph->tot_len));
    printk("IP:id=%d\n",u16toValue(skb->nh.iph->id));
    printk("IP:frag_off=%d\n",u16toValue(skb->nh.iph->frag_off));
    printk("IP:ttl=%d\n",skb->nh.iph->ttl);
    printk("IP:protocol=0x%X\n",skb->nh.iph->protocol);
    printk("IP:check=0x%04X\n",skb->nh.iph->check);
    IpAddressToString(u32toValue(skb->nh.iph->saddr),SourceIP);
    IpAddressToString(u32toValue(skb->nh.iph->daddr),TargetIP);
    printk("IP:saddr=%s\n",SourceIP);
    printk("IP:daddr=%s\n",TargetIP);
    
    if(iph->protocol == IPPROTO_TCP) 
    {
        th = skb->h.th;
    	printk("TCP:Source Port=%u\n",u16toValue(th->source));        
    	printk("TCP:Destination Port=%u\n",u16toValue(th->dest));
    	printk("TCP:seq=%u\n",u32toValue(th->seq));
    	printk("TCP:ack_seq=%u\n",u32toValue(th->ack_seq));
    	printk("TCP:window=0x%04X\n",u16toValue(th->window));
    	printk("TCP:check=0x%04X\n",u16toValue(th->check));
    	printk("TCP:urg_ptr=0x%04X\n",u16toValue(th->urg_ptr));
    }    
    
    return 0;
}
int init_module(void)
{
    eeFunction02  = PacketInformation;	
    printk("\neeFunction2: PacketInformation install ok!\n");
    return 0;
}
void cleanup_module(void)
{
    eeFunction02 = 0;
    printk("\neeFunction2: PacketInformation uninstall ok!\n");
}
MODULE_LICENSE("GPL");


